//
//  AppDelegate.swift
//  JDWeiBo
//
//  Created by apple on 15/6/23.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import AFNetworking

var sharedUserAccount = JDUserAccount.loadAccount()

let JDSwitchRootVCNotification = "JDSwitchRootVCNotification"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // 测试单例
//        print(JDNetworkTools.sharedNetworkTools())
//        print(JDNetworkTools.sharedNetworkTools())
        
        // 设置全局外观
        setupAppearance()
        
        // 设置网络
        setupNetwork()
       
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
       window?.rootViewController = defaultViewController()
//        window?.rootViewController = JDMainViewController()
        
        window?.makeKeyAndVisible()
        
        // 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: JDSwitchRootVCNotification, object: nil)
        
        return true
    }
    
    ///  切换控制器
    func switchViewController(n: NSNotification) {
        // 判断 object 是否为 true，如果是，显示 MainViewController
        let isMainVC = n.object as! Bool
        print("-------------\(isMainVC)")
        
        window?.rootViewController = isMainVC ? JDMainViewController() : JDWelcomeViewController()
        print(window?.rootViewController)
    }
    
    ///  判断加载的默认控制器
    private func defaultViewController() -> UIViewController {
        
        // 1. 判断用户是否登录
        if sharedUserAccount != nil {
            
            // 2. 如果登录，判断是否有新版本
            return isNewUpdate() ? JDNewFeatureViewController() : JDWelcomeViewController()
        }
        
        return JDMainViewController()
    }
    
    /// 是否新版本
    private func isNewUpdate() -> Bool {
        
        // 1. 获取应用程序`当前版本`
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = NSNumberFormatter().numberFromString(currentVersion)!.doubleValue
        print(version)
        
        // 2. 获取应用程序`之前的版本`，从用户偏好中读取
        let versionKey = "versionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        print(sandBoxVersion)
        
        // 3. 将`当前版本`写入用户偏好
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: versionKey)
        // 从 Xcode 6.0 开始就不需要同步了
        // NSUserDefaults.standardUserDefaults().synchronize()
        
        return version > sandBoxVersion
    }
    
    // 设置网络
    private func setupNetwork() {
        
        // 设置网路指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        
        // 设置网络缓存
        let urlCache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        
        // 得到网络缓存信息
        NSURLCache.setSharedURLCache(urlCache)
    }
    
    // 抽取一个类 设置全局外观
    private func setupAppearance() {
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }

}

